<?php
class MPackage {
    
    const TYPE_CSS = 'css';
    const TYPE_JAVASCRIPT = 'javascript';
    
    public static function import($packageName){
        if (array_key_exists($packageName, self::$includedPackages)){
            $package = self::$includedPackages[$packageName];
            if (array_key_exists($packageName, self::$dependencies)){
                $packageDependencies = self::$dependencies[$packageName];
                foreach ($packageDependencies as $dependency){
                    $dep = self::$includedPackages[$dependency];
                    if ($dep['type'] === self::TYPE_JAVASCRIPT){
                        MTemplate::getInstance()->addScript(MPublisher::publish(__DIR__.DS.'src'.DS.$dep['src']));
                    } else if ($dep['type'] === self::TYPE_CSS) {
                        MTemplate::getInstance()->addStylesheet(MPublisher::publish(__DIR__.DS.'src'.DS.$dep['src']));
                    }
                }
            }
            if (isset ($package['dependencies']) && !empty($package['dependencies'])){
                $packageDependencies = $package['dependencies'];
                foreach ($packageDependencies as $dependency){
                    if ($dependency['type'] === self::TYPE_CSS){
                        if (isset ($dependency['src'])){
                            if (is_dir(__DIR__.DS.'src'.DS.$dependency['src'])){
                                MPublisher::publish(__DIR__.DS.'src'.DS.$dependency['src']);
                                if (isset ($dependency['srcFile'])){
                                    MTemplate::getInstance()->addStylesheet(MPublisher::publish(__DIR__.DS.'src'.DS.$dependency['srcFile']));
                                }
                            }
                            else
                                MTemplate::getInstance()->addStylesheet(MPublisher::publish(__DIR__.DS.'src'.DS.$dependency['src']));
                        }
                        
                    }
                }
            }
            if ($package['type'] === self::TYPE_JAVASCRIPT){
                MTemplate::getInstance()->addScript(MPublisher::publish(__DIR__.DS.'src'.DS.$package['src']));
            } else if ($package['type'] === self::TYPE_CSS) {
                MTemplate::getInstance()->addStylesheet(MPublisher::publish(__DIR__.DS.'src'.DS.$package['src']));
            }
        }
    }
    
    public static $includedPackages = array(
        'mandrill-base-css'=>array(
            'src'=>'mandrill/ml.base.css',
            'type'=>self::TYPE_CSS,
        ),
        'mandrill-box-css'=>array(
            'src'=>'mandrill/ml.box.css',
            'type'=>self::TYPE_CSS,
        ),
        'mandrill-form-css'=>array(
            'src'=>'mandrill/ml.form.css',
            'type'=>self::TYPE_CSS,
        ),
        'mandrill-pager-css'=>array(
            'src'=>'mandrill/ml.pager.css',
            'type'=>self::TYPE_CSS,
        ),
        'mandrill-table-css'=>array(
            'src'=>'mandrill/ml.table.css',
            'type'=>self::TYPE_CSS,
        ),
        'mandrill-widget'=>array(
            'src'=>'mandrill/ml.widget.js',
            'type'=>self::TYPE_JAVASCRIPT,
            'dependencies'=>array(
                'css'=>array(
                    'src'=>'mandrill/widget',
                    'srcFile'=>'mandrill/widget/ml.widget.css',
                    'type'=>self::TYPE_CSS,
                ),
            ),
        ),
        'jquery'=>array(
            'src'=>'jquery/jquery.min.js',
            'type'=>self::TYPE_JAVASCRIPT,
            'version'=>'1.4.2',
        ),
        'jquery-ui'=>array(
            'src'=>'jquery/jquery-ui.min.js',
            'type'=>self::TYPE_JAVASCRIPT,
            'version'=>'1.8.4',
            'dependencies'=>array(
                'css'=>array(
                    'src'=>'jquery/css/smoothness',
                    'srcFile'=>'jquery/css/smoothness/jquery-ui.css',
                    'type'=>self::TYPE_CSS,
                )
            ),
        ),
        'jquery-datatables'=>array(
            'src'=>'jquery/jquery.dataTables.min.js',
            'type'=>self::TYPE_JAVASCRIPT,
            'version'=>'1.7.0',
        ),
        'jquery-form'=>array(
            'src'=>'jquery/jquery.form.js',
            'type'=>self::TYPE_JAVASCRIPT,
        ),
        'jquery-editor'=>array(
            'src'=>'jquery/jquery.cleditor.min.js',
            'type'=>self::TYPE_JAVASCRIPT,
            'version'=>'1.2.2',
            'dependencies'=>array(
                'css'=>array(
                    'src'=>'jquery/css/cleditor',
                    'srcFile'=>'jquery/css/cleditor/jquery.cleditor.css',
                    'type'=>self::TYPE_CSS,
                )
            ),
        ),
        'jquery-colorbox'=>array(
            'src'=>'jquery/jquery.colorbox.min.js',
            'type'=>self::TYPE_JAVASCRIPT,
            'version'=>'1.3.9',
            'dependencies'=>array(
                'css'=>array(
                    'src'=>'jquery/css/colorbox',
                    'srcFile'=>'jquery/css/colorbox/colorbox.css',
                    'type'=>self::TYPE_CSS,
                )
            ),
        ),
        'formee'=>array(
            'src'=>'formee/css/form-structure.css',
            'type'=>self::TYPE_CSS,
            'version'=>'1.2',
            'dependencies'=>array(
                'css'=>array(
                    'src'=>'formee/css',
                    'srcFile'=>'formee/css/form-style.css',
                    'type'=>self::TYPE_CSS,
                ),
            ),
        ),
    );
    
    public static $dependencies = array(
        'jquery-ui'=>array(
            'jquery',
        ),
        'jquery-colorbox'=>array(
            'jquery',
        )
    );
}
?>
